
import {
  collectTypeList
} from "../../utils/commondata.js";

import {
  request
} from "../../utils/request.js";
import regeneratorRuntime from '../../utils/runtime';



Page({

  /**
   * 页面的初始数据
   */
  data: {
    TabCur: 0,
    scrollLeft:0,
    collectType:'famous',
    page:1,
    collectTypeList:collectTypeList(),
    contentList:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.loadCollectData(1,this.data.collectType);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.loadCollectData(1,this.data.collectType);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let page = this.data.page+1;
    this.loadCollectData(page,this.data.collectType);
  },

  /**
   * 
   * 列表切换
   * @param {*} e 
   */
  tabSelect(e) {
    this.setData({
      TabCur: e.currentTarget.dataset.index,
      scrollLeft: (e.currentTarget.dataset.index-1)*60,
      contentList:[],
    });
    this.loadCollectData(1,this.data.collectTypeList[e.currentTarget.dataset.index].key);
  },
  /**
   * 
   * 显示明细
   * @param {*} e 
   */
  handleShowDetail(e){
    let {item} = e.currentTarget.dataset;
    let {TabCur,collectTypeList} = this.data;
    let type = collectTypeList[TabCur].key;
    console.log(item);
    let url = '';
    if( type == 'author'){
      url = '/pages/author/detail/index?authorId=' + item.id;
    }else if(type == 'famous'){
      url = '/pages/chapter/detail/index?contentType=poetry&chapterId=' + item.parentId ;
    }
    else if(type == 'book'){
      url = '/pages/book/detail/index?bookId=' + item.id + "&bookName=" +item.title;
    }
    else if(type == 'poetry'){
      url = '/pages/chapter/detail/index?contentType=poetry&chapterId=' + item.id ;
    }
    console.log(url);
    wx.navigateTo({
      url: url
    });


  },
  /**
   * 加载收藏的数据
   */
  async loadCollectData(page,type){
    let openId = wx.getStorageSync("openId");
    const res = await request({
      url: "/collect/pageQuery?contentType=" + type 
      + "&page=" + page + "&openId=" + openId,
      method: "get"
    });
    if(res.code != 0){
      return;
    }
    this.setData({
      contentList:[...this.data.contentList,...res.data.list],
      page:page,
      contentType:type
    });
  },

})